package com.xu.seal.utils;

import java.io.*;
import java.util.Random;

public class NameUtils {

    public static void main(String[] args) {
        System.out.println(new NameUtils().createRandomName());
    }

    public String createRandomName() {

        String companyName = System.getProperty("user.dir") + "/img/name.txt";
        String companyType = System.getProperty("user.dir") + "/img/type.txt";
//        System.out.println(companyName);
//        System.out.println(companyType);

        String[] nameArrays = readFile(companyName);
        String[] typeArrays = readFile(companyType);

        String company = getRandomElement(nameArrays);
        String type = getRandomElement(typeArrays);
        return company + type;
    }

    public void writeFile(String filePath, String content) {
        File file = new File(System.getProperty("user.home") + "/test.txt");
        try {
            FileWriter writer = new FileWriter(file);
            writer.write("Hello, Mac");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String[] readFile(String filePath) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = reader.readLine()) != null) {
                // 解析每一行的内容
                stringBuilder.append(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString().split("、");
    }

    public String getRandomElement(String[] array) {
        Random rand = new Random();
        int index = rand.nextInt(array.length); // 生成一个[0, array.length)范围内的随机索引
        return array[index]; // 返回随机索引对应的元素
    }
}
